package by.ostrovsky.webhousingsystem.bean.data;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import by.ostrovsky.webhousingsystem.bean.TemplateDataBean;
import by.ostrovsky.webhousingsystem.controller.IOperateController;
import by.ostrovsky.webhousingsystem.controller.employe.DeleteEmployeeController;
import by.ostrovsky.webhousingsystem.controller.employe.GetEmployeController;
import by.ostrovsky.webhousingsystem.controller.employe.UpdateEmployeController;
import by.ostrovsky.webhousingsystem.exception.CommonException;
import by.ostrovsky.webhousingsystem.exception.EmptyParameterException;
import by.ostrovsky.webhousingsystem.exception.OperationErrorException;
import by.ostrovsky.webhousingsystem.hibernate.entity.Employee;
import by.ostrovsky.webhousingsystem.hibernate.entity.TaskGroup;

@SuppressWarnings("serial")
public class EmployeBean extends TemplateDataBean<Employee> {
	private static Logger logger = Logger.getLogger(EmployeBean.class);

	private String[] name = null;
	private String[] surname = null;
	private String[] isMaster = null;
	private String[] isBusy = null;
	private String[] freeAt = null;
	private String[] speciality = null;

	private void initSelectedEntitiesList() throws OperationErrorException {
		selectedEntities = new LinkedList<Employee>();
		Employee empl = new Employee();
		try {
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			if (freeAt[Integer.parseInt(selectedEllements[0])] != "")
				date = dateFormat.parse(freeAt[Integer
						.parseInt(selectedEllements[0])]);
			boolean isBusyBoolean = Boolean.parseBoolean(isBusy[Integer
					.parseInt(selectedEllements[0])]);
			boolean isMasterBoolean = Boolean.parseBoolean(isMaster[Integer
					.parseInt(selectedEllements[0])]);

			empl.setId(Long.parseLong(id[Integer.parseInt(selectedEllements[0])]));
			empl.setName(name[Integer.parseInt(selectedEllements[0])]);
			empl.setSurname(surname[Integer.parseInt(selectedEllements[0])]);
			empl.setSpeciality(speciality[Integer
					.parseInt(selectedEllements[0])]);
			empl.setFreeAt(date);
			empl.setIsMaster(isMasterBoolean);
			empl.setIsBusy(isBusyBoolean);

			selectedEntities.add(empl);
		} catch (Exception e) {
			logger.error(e);
			throw new OperationErrorException();
		}
	}

	public EmployeBean() {
		try {
			read();
		} catch (CommonException e) {
			logger.error(e);
		}
	}
	
	public void initAndRetreat(){
		initSelected();
		try {
			sessionMap.put("loadNext", "wizardwork");
			sessionMap.put("employeBean", EmployeBean.this);
			String source = ServletActionContext.getRequest().getHeader("REFERER");
			ServletActionContext.getResponse().sendRedirect(source);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Override
	public void delete() throws CommonException {
		initSelected();
		if (selectedEntities.isEmpty())
			throw new EmptyParameterException();
		IOperateController<Employee, Boolean> controller = new DeleteEmployeeController();
		controller.delete(selectedEntities);
		read();
		try {
			sessionMap.put("reloadPrevContent", "employee");
			sessionMap.put("employeBean", EmployeBean.this);
			String source = ServletActionContext.getRequest().getHeader("REFERER");
			ServletActionContext.getResponse().sendRedirect(source);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Override
	public void update() throws CommonException {
		if(selectedEllements == null)
			throw new EmptyParameterException();
		initSelectedEntitiesList();
		if (selectedEntities.isEmpty())
			throw new EmptyParameterException();
		IOperateController<Employee, Boolean> controller = new UpdateEmployeController();
		controller.set(selectedEntities);
		read();
		try {
			sessionMap.put("reloadPrevContent", "employee");
			sessionMap.put("employeBean", EmployeBean.this);
			String source = ServletActionContext.getRequest().getHeader("REFERER");
			ServletActionContext.getResponse().sendRedirect(source);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Override
	public void read() throws CommonException {
		entityList = new LinkedList<Employee>();
		IOperateController<Employee, Boolean> controller = new GetEmployeController();
		entityList.addAll(controller.get());
	}

	public void appointEmployes(TaskGroup tg) throws OperationErrorException {
		// initSelected() is not nessessary for here!
		// this method would not be called directly form view
		// WizardAction give needed preparation
		try {
			for (Employee empl : selectedEntities) {
				empl.setFreeAt(tg.getFreeAt());
				empl.setIsBusy(true);
				empl.setTaskGroup(tg);
			}
		} catch (Exception e) {
			logger.error(e);
			throw new OperationErrorException();
		}
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String[] getName() {
		return name;
	}

	public void setName(String[] name) {
		this.name = name;
	}

	public String[] getSurname() {
		return surname;
	}

	public void setSurname(String[] surname) {
		this.surname = surname;
	}

	public String[] getIsMaster() {
		return isMaster;
	}

	public void setIsMaster(String[] isMaster) {
		this.isMaster = isMaster;
	}

	public String[] getIsBusy() {
		return isBusy;
	}

	public void setIsBusy(String[] isBusy) {
		this.isBusy = isBusy;
	}

	public String[] getFreeAt() {
		return freeAt;
	}

	public void setFreeAt(String[] freeAt) {
		this.freeAt = freeAt;
	}

	public String[] getSpeciality() {
		return speciality;
	}

	public void setSpeciality(String[] speciality) {
		this.speciality = speciality;
	}
}
